using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace manySquares
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D playingPieces;
        Board board;
        Rectangle changeButton;
        Rectangle resetButton;
        Player player1;
        const float MinTimeSinceLastInput = .5f;
        float timeSinceLastInput = 0.0f;
        int curretX=-1;
        int curretY = -1;

        Menu menu = new Menu();

        enum GameStates
        {
            TitleScreen, Playing,Minigame
        };
        enum TurnStates
        {
            Move, Build, Attack
        };
        GameStates gameStates = GameStates.TitleScreen;
        TurnStates turnStates = TurnStates.Move;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            board = new Board(11, 11);
            changeButton = new Rectangle(500,200,100,50);
            resetButton = new Rectangle(500, 100, 100, 50);
            this.Window.AllowUserResizing = true;
            IsMouseVisible = true;
            graphics.PreferredBackBufferWidth =800;
            graphics.PreferredBackBufferHeight =600;
            graphics.ApplyChanges();
            player1 = new Player(GamePad.GetState(PlayerIndex.One), "Gamer");
            
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            playingPieces = Content.Load<Texture2D>("Tile_Sheet");
            //menu.background = Content.Load<Texture2D>("background");
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            timeSinceLastInput += (float)gameTime.ElapsedGameTime.TotalSeconds;

            gameStates = GameStates.TitleScreen;
           
            switch (gameStates)    //Spencer
            {
                case GameStates.Playing:
                    if (timeSinceLastInput >= MinTimeSinceLastInput)
                    {
                        HandleMouseInput(Mouse.GetState());
                    }
                    break;
                case GameStates.TitleScreen:
                    Menu menu = new Menu();

                    break;
                case GameStates.Minigame:
                    break;
            }
            // TODO: Add your update logic here
            IsMouseVisible = true;
            base.Update(gameTime);
        }
        private void HandleMouseInput(MouseState mouseState)
        {
            board.resetAll();
            if (curretX != -1 && curretY != -1)
            {
                board.selectSpace(curretX, curretY, Color.Yellow);
            }
            int x = ((mouseState.X )/40);
            int y = ((mouseState.Y ) / 40);
            if (resetButton.Contains(mouseState.X, mouseState.Y) && mouseState.LeftButton == ButtonState.Pressed)
            {
                if (curretX != -1 && curretY != -1)
                {
                    switch (turnStates)
                    {
                        case TurnStates.Move:
                            turnStates = TurnStates.Attack;
                            break;
                        case TurnStates.Attack:
                            turnStates = TurnStates.Move;
                            break;
                    }
                }

            }
            if (changeButton.Contains(mouseState.X, mouseState.Y) && mouseState.LeftButton == ButtonState.Pressed)
            {
                if (curretX != -1 && curretY != -1)
                {
                    board.changeRect(curretX, curretY);
                    board.addUnit(curretX, curretY);
                }
            }
            if ((x >= 0) && (x < board.x) && (y >= 0) && (y < board.y))
            {
                if (mouseState.LeftButton == ButtonState.Pressed)
                {
                    if (board.GetColor(x, y) == Color.White)
                    {
                        curretX = x;
                        curretY = y;
                    }
                    switch(turnStates){
                        case TurnStates.Move:
                            if (board.GetColor(x, y) == Color.Green&&board.GetUnit(x,y)==0)
                            {
                                board.resetRect(curretX, curretY);
                                board.changeRect(x, y);
                                board.addUnit(x, y);
                                board.removeUnit(curretX, curretY);
                            }
                        break;
                        case TurnStates.Attack:
                            if (board.GetColor(x, y) == Color.Red && board.GetUnit(x, y) == 1)
                            {
                                board.removeUnit(x, y);
                            }
                            if (board.GetColor(x, y) == Color.Green && board.GetUnit(x, y) == 0)
                            {
 
                            }
                        break;
                    }
                }
            }
            
        }
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);
            spriteBatch.Begin();
            switch (gameStates)
            {
                case GameStates.Playing:
                    this.Window.Title = player1.Name+" :$"+player1.Money;
                        for (int x = 0; x < board.x; x++)
                        {
                            for (int y = 0; y < board.y; y++)
                            {
                                spriteBatch.Draw(playingPieces, new Rectangle(40 * x, 40 * y, 40, 40),new Rectangle(1,247,40,40), board.GetColor(x, y));
                            }
                        }
                        for (int x = 0; x < board.x; x++)
                        {
                            for (int y = 0; y < board.y; y++)
                            {
                                spriteBatch.Draw(playingPieces, new Rectangle(40*x, 40*y,40, 40), board.GetSourceRect(x,y), board.GetColor(x,y));
                            }
                        }
                        spriteBatch.Draw(playingPieces, resetButton, new Rectangle(1,247,40,40), Color.Green);
                        spriteBatch.Draw(playingPieces, changeButton, new Rectangle(1, 247, 40, 40), Color.Red);
                    break;
                case GameStates.TitleScreen:
                    //spriteBatch.Draw(menu.background, new Rectangle(0, 0, 800, 600), Color.White);
                    Console.Write(GraphicsDevice.Viewport.Width);
                    break;
            }
            spriteBatch.End();
            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
